package com.htjc.integral.account.util;



/**
 * 埃特巴什码：最后一个字母代表第一个字母，倒数第二个字母代表第二个字母
 * 
 * @author lqm
 * @date 2016/12/2
 */
public class Atbash {

	private static final String NORMAL_SEQ = "ABCDEFGHIJKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890=";
	private static final String    PWD_SEQ = "=0987654321ABCDEFGHIJKMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba";
	private static final String NORMAL_SEQ_TERMINAL = "@ABCDEFGHIJKMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890-";
	private static final String    PWD_SEQ_TERMINAL = "-0987654321ABCDEFGHIJKMNOPQRSTUVWXYZzyxwvutsrqponmlkjihgfedcba@";

	/**
	 * 解密
	 * 
	 * @param str
	 * @return
	 */
	public static String decrypt(String str) {
		StringBuffer result = new StringBuffer();
		if (!str.trim().equals("")) {
			for (int i = 0; i < str.length(); i++) {
				int index = NORMAL_SEQ.indexOf(str.charAt(i));
				if (index != -1) {
					result.append(PWD_SEQ.charAt(index));
				} else {
					result.append(str.charAt(i));
				}
			}
		}
		return result.toString();
	}
	

	/**
	 * 加密
	 * 
	 * @param str
	 * @return
	 */
	public static String encrypt(String str) {
		StringBuffer result = new StringBuffer();
		if (!str.trim().equals("")) {
			for (int i = 0; i < str.length(); i++) {
				int index = PWD_SEQ.indexOf(str.charAt(i));
				if (index != -1) {
				result.append(NORMAL_SEQ.charAt(index));
				} else {
					result.append(str.charAt(i));
				}
			}
		}
		return result.toString();
	}
	
	/**
	 * 終端解密
	 * 
	 * @param str
	 * @return
	 */
	public static String decryptTerminal(String str) {
		StringBuffer result = new StringBuffer();
		if (!str.trim().equals("")) {
			for (int i = 0; i < str.length(); i++) {
				int index = NORMAL_SEQ_TERMINAL.indexOf(str.charAt(i));
				if (index != -1) {
					result.append(PWD_SEQ_TERMINAL.charAt(index));
				} else {
					result.append(str.charAt(i));
				}
			}
		}
		return result.toString();
	}
	/**
	 * 終端加密
	 * 
	 * @param str
	 * @return
	 */
	public static String encryptTerminal(String str) {
		StringBuffer result = new StringBuffer();
		if (!str.trim().equals("")) {
			for (int i = 0; i < str.length(); i++) {
				int index = PWD_SEQ_TERMINAL.indexOf(str.charAt(i));
				if (index != -1) {
				result.append(NORMAL_SEQ_TERMINAL.charAt(index));
				} else {
					result.append(str.charAt(i));
				}
			}
		}
		return result.toString();
	}
	public static String dencrytp(String value,String msg){
		try{
			String strdec = new String(Base64.decode(Atbash.decryptTerminal(value)));
			return strdec;
		}catch(Exception e){
			throw new RuntimeException("解密"+msg+"失败");
		}
	}

	public static void main(String[] args) {
			System.out.println(AtbashUtils.encrypt("测试账号"));
			System.out.println(AtbashUtils.decrypt("GthLFW+gFLdyGj+I"));
	}
}
